using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Application.Services
{
    /// <summary>
    /// 双因子身份认证应用服务。
    /// 提供双因子身份认证密钥生成、令牌验证、备份码验证和重新生成等功能。
    /// </summary>
    [JGSY.CMS.LowCode.Platform.Infrastructure.Services.LogOperation]
    public class TwoFactorAppService : ITwoFactorAppService{
        /// <summary>
        /// 双因子认证服务接口
        /// </summary>
        private readonly ITwoFactorAppService _service;
        
        /// <summary>
        /// 构造函数，注入双因子认证服务
        /// </summary>
        /// <param name="service">双因子认证服务接口</param>
        public TwoFactorAppService(ITwoFactorAppService service)
        {
            _service = service;
        }
        
        /// <summary>
        /// 为用户生成双因子认证密钥
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>双因子认证设置结果</returns>
        public async Task<TwoFactorSetupResultDto> GenerateTwoFactorSecretAsync(string username)
        {
            return await _service.GenerateTwoFactorSecretAsync(username);
        }
        
        /// <summary>
        /// 验证双因子认证令牌
        /// </summary>
        /// <param name="token">用户输入的验证码</param>
        /// <param name="secret">双因子认证密钥</param>
        /// <returns>验证是否成功</returns>
        public async Task<bool> VerifyTokenAsync(string token, string secret)
        {
            return await _service.VerifyTokenAsync(token, secret);
        }
        
        /// <summary>
        /// 验证备份验证码
        /// </summary>
        /// <param name="code">用户输入的备份验证码</param>
        /// <param name="backupCodes">系统存储的备份验证码数组</param>
        /// <returns>验证结果</returns>
        public async Task<TwoFactorVerifyResultDto> VerifyBackupCodeAsync(string code, string[] backupCodes)
        {
            return await _service.VerifyBackupCodeAsync(code, backupCodes);
        }
        
        /// <summary>
        /// 重新生成备份验证码
        /// </summary>
        /// <returns>新的备份验证码数组</returns>
        public async Task<string[]> RegenerateBackupCodesAsync()
        {
            return await _service.RegenerateBackupCodesAsync();
        }
        
        /// <summary>
        /// 获取双因子认证摘要信息
        /// </summary>
        /// <param name="data">双因子认证数据</param>
        /// <returns>双因子认证摘要</returns>
        public async Task<TwoFactorSummaryDto> GetTwoFactorSummaryAsync(TwoFactorDataDto data)
        {
            return await _service.GetTwoFactorSummaryAsync(data);
        }
    }
}
